﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace WpfPopup
{
    /// <summary>
    /// Interaction logic for CustomTooltipControl.xaml
    /// </summary>
    public partial class CustomTooltipControl : UserControl
    {
        #region constructor
        public CustomTooltipControl()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(CustomTooltipControl_Loaded);
        }
        #endregion

        #region PlaceTarget
        public FrameworkElement PlaceTarget
        {
            get { return (FrameworkElement)this.GetValue(PlaceTargetProperty); }
            set { SetValue(PlaceTargetProperty, value); }
        }

        public static readonly DependencyProperty PlaceTargetProperty =
            DependencyProperty.Register("PlaceTarget", typeof(FrameworkElement), typeof(CustomTooltipControl));
        #endregion

        #region Tooltip for popup
        public string TooltipString
        {
            get { return (string)this.GetValue(TooltipStringProperty); }
            set { SetValue(TooltipStringProperty, value); }
        }

        public static readonly DependencyProperty TooltipStringProperty =
            DependencyProperty.Register("TooltipString", typeof(string), typeof(CustomTooltipControl));
        #endregion

        #region event
        void CustomTooltipControl_Loaded(object sender, RoutedEventArgs e)
        {
            #region find window
            DependencyObject dependObject = this;
            bool isWindow = LogicalTreeHelper.GetParent(dependObject) is Window;
            while (!isWindow)
            {
                dependObject = LogicalTreeHelper.GetParent(dependObject);
                isWindow = dependObject is Window;
            }
            var win = dependObject as Window;
            if (win != null)
            {
                win.LocationChanged += new EventHandler(win_LocationChanged);
            }
            #endregion

            #region set tooltip string
            if (PlaceTarget != null)
            {
                if (PlaceTarget.ToolTip != null)
                {
                    this.TooltipString = PlaceTarget.ToolTip.ToString();
                    PlaceTarget.ToolTip = null;
                }
                else
                {
                    pop.IsOpen = pop.StaysOpen = false;
                }
            }
            #endregion
        }

        void win_LocationChanged(object sender, EventArgs e)
        {
            var mi = typeof(Popup).GetMethod("UpdatePosition", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            mi.Invoke(pop, null);
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            pop.IsOpen = pop.StaysOpen = false;
            PlaceTarget.ToolTip = this.TooltipString;
        }
        #endregion
    }

    #region convert offset to margin
    public class OffsetConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return 0;
            double offset = 0;
            double.TryParse(value.ToString(), out offset);
            return -offset / 2;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    #endregion
}
